package com.gaogzhen.algorithm.leetCode;

public class IsUgly {
    public static void main(String[] args) {
        int n = 50;
        System.out.println(isUgly(n));
    }

    public static boolean isUgly(int n) {
        if (n == 1 || n == 2 || n == 3 || n == 5) return true;
        while ((n % 5) == 0) {
            n /= 5;
            if (n == 1)
                return true;
        }
        while ((n % 3) == 0) {
            n /= 3;
            if (n == 1)
                return true;
        }
        while ((n % 2) == 0) {
            n /= 2;
            if (n == 1)
                return true;
        }
        return false;
    }

    public  static boolean isUgly1(int n) {
        if (n <= 0) return false;
        while (true) {
            if (n == 1) return true;
            if (n % 2 == 0) n >>= 1;
            else if(n % 3 == 0) n /= 3;
            else if(n % 5 == 0) n /= 5;
            else return false;
        }
    }
}
